package com.zzy.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;

import java.util.Properties;

public class SaftyEnvironmentPostProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        String enusername = environment.getProperty("spring.datasource.username");//获取配置文件加密的用户名
        System.out.println("获取到加密的用户名："+enusername);
        if(enusername!=null && enusername.trim().length()>0){
            System.out.println("执行解密。。。："+enusername );
            String deusername = "root";//解密操作
            Properties properties = new Properties();
            properties.setProperty("spring.datasource.username",deusername);
            PropertiesPropertySource ps = new PropertiesPropertySource("spring.datasource.username",properties);
            environment.getPropertySources().addFirst(ps);
        }
    }
}
